//
//  NetRequestLimiter.m
//  KidReading
//
//  Created by YangShuai on 17/2/15.
//  Copyright © 2017年 刘赞黄Telen. All rights reserved.
//

#import "NetRequestLimiter.h"
#import "ConstDefine.h"

@implementation NetRequestLimiter{
    BOOL _isOneLifeCycleLimited_HomePage;
    BOOL _isOneLifeCycleLimited_Library;
    BOOL _isOneLifeCycleLimited_SpecialLibrary;
}
static NetRequestLimiter *singleLimiter = nil;
+ (NetRequestLimiter*)defaultLimiter{
    if (!singleLimiter) {
        @synchronized (self) {
            singleLimiter = [[NetRequestLimiter alloc] init];
        }
    }
    return singleLimiter;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        _isOneLifeCycleLimited_Library = NO;
        _isOneLifeCycleLimited_SpecialLibrary = NO;
        _isOneLifeCycleLimited_HomePage = NO;
    }
    return self;
}

/** 获取是否还在原来的生命周期内 */
-(BOOL)isOneLifeCycleLimitedWithLimitedPage:(OneCycleLimitedPage)limitedPage{
    if (limitedPage == OneCycleLimitedPage_Library) {
        return _isOneLifeCycleLimited_Library;
    }else if (limitedPage == OneCycleLimitedPage_HomePage){
        return _isOneLifeCycleLimited_HomePage;
    }else if (limitedPage == OneCycleLimitedPage_SpecialLibrary){
        return _isOneLifeCycleLimited_SpecialLibrary;
    }
    return NO;
}
/** 获取首页或者书库数据成功后,更新限制 */
-(void)updateOneLifeCycleLimitedWithLimitedPage:(OneCycleLimitedPage)limitedPage{
    if (limitedPage == OneCycleLimitedPage_Library) {
        _isOneLifeCycleLimited_Library = YES;
    }else if (limitedPage == OneCycleLimitedPage_HomePage){
        _isOneLifeCycleLimited_HomePage = YES;
    }else if (limitedPage == OneCycleLimitedPage_SpecialLibrary){
        _isOneLifeCycleLimited_SpecialLibrary = YES;
    }
}

/** 清除上一个生命周期标识 */
-(void)cleanOneLifeCycleLimitedWithLimitedPage:(OneCycleLimitedPage)limitedPage{
    if (limitedPage == OneCycleLimitedPage_Library) {
        _isOneLifeCycleLimited_Library = NO;
    }else if (limitedPage == OneCycleLimitedPage_HomePage){
        _isOneLifeCycleLimited_HomePage = NO;
    }else if (limitedPage == OneCycleLimitedPage_SpecialLibrary){
        _isOneLifeCycleLimited_SpecialLibrary = NO;
    }
}
-(void)cleanAllOneLifeCycleLimited{
    _isOneLifeCycleLimited_Library = NO;
    _isOneLifeCycleLimited_HomePage = NO;
    _isOneLifeCycleLimited_SpecialLibrary = NO;
}

/** 获取是否存在一天的限制 */
-(BOOL)isOneDayLimitedWithCacheTimestamp:(NSTimeInterval)cacheTimestamp{
    cacheTimestamp /= 1000;
    BOOL isOneDayLimited = YES;
    NSString* currentDateString = [NSDate stringFromDate:[NSDate date] withFormat:@"yyyy-MM-dd"];
    NSString* cacheDataString = [NSDate stringFromDate:[NSDate dateWithTimeIntervalSince1970:cacheTimestamp] withFormat:@"yyyy-MM-dd"];
    if (![currentDateString isEqualToString:cacheDataString]) {
        isOneDayLimited = NO;
    }
    return isOneDayLimited;
}

@end
